package com.lovo.javafxComponent;

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;

/**
 * 分页插件
 * @author lenovo
 *
 */
public abstract class LovoCutPage {
	private int pageNO = 1;
    private int totalPage;
	private TextField pageTxt = new TextField("1");
	private Label totalLabel = new Label("/");

	public LovoCutPage(int x,int y,Pane container) {
		new CutPageButton("<<", x, y, container);
		new CutPageButton("<", x+60, y, container);
		
		pageTxt.setLayoutX(x+120);
		pageTxt.setLayoutY(y);
		pageTxt.setPrefSize(50, 40);
		pageTxt.setFont(new Font(20));
		
		totalLabel.setLayoutX(x+170);
		totalLabel.setLayoutY(y);
		totalLabel.setFont(new Font(30));
		
		CutPageButton goButton = new CutPageButton("GO", x+230, y, container);
		goButton.setFont(new Font(10));
		new CutPageButton(">", x+290, y, container);
		new CutPageButton(">>", x+350, y, container);
		
		container.getChildren().add(pageTxt);
		container.getChildren().add(totalLabel);
	}
	
	private class CutPageButton extends Button{
		public CutPageButton(String info,int x,int y,Pane container){
			this.setBorder(this.getViewBorder());
			this.setText(info);
			this.setFont(new Font(13));
			this.setBackground(new Background(new BackgroundFill(new Color(0.01,0.6,0.917,1), new CornerRadii(20), new Insets(3))));
			this.setLayoutX(x);
			this.setLayoutY(y);
			this.setPrefSize(40, 40);
			this.setOnAction(e -> pageNOChange(info));
			this.setTextFill(Paint.valueOf("#ffffff"));
            this.setStyle("-fx-font-weight:bold");
			container.getChildren().add(this);
		}
		
		private Border getViewBorder() {
			return  new Border(new BorderStroke(Paint.valueOf("#000"), 
					BorderStrokeStyle.SOLID, new CornerRadii(20), new BorderWidths(1)));
		}
	}
	
	
    /**
     * 设置总页数
     * @param total 总页数
     */
    public void setTotal(int total) {
        this.totalPage = total;;
        pageNO = 1;
        pageTxt.setText("1");

        totalLabel.setText("/"+totalPage);
    }
    
    private void pageNOChange(String info){
        if(info.equals("<")){
            pageNO --;
            if(pageNO < 1){
                pageNO = 1;
            }
        }
        else if(info.equals("<<")){
            pageNO = 1;
        }
        else if(info.equals(">")){
            pageNO ++;
            if(pageNO > totalPage){
                pageNO = totalPage;
            }
        }
        else if(info.equals(">>")){
            pageNO = totalPage;
        }
        else if(info.equals("GO")){
            if(!pageTxt.getText().matches("\\d+")){
                pageTxt.setText("1");
            }

            pageNO = Integer.parseInt(pageTxt.getText());
            if(pageNO < 1){
                pageNO = 1;
            }
            else if(pageNO > totalPage){
                pageNO = totalPage;
            }
        }
        this.pageTxt.setText(pageNO+"");
        this.clickEvent(pageNO);
    }

    /**
     * 事件方法
     * @param pageNO 页码
     */
    public abstract void clickEvent(int pageNO);

}
